package Euler2;

import java.util.LinkedList;

public class FibSum {

	public static void main(String[] args) 
	{
		long sum = 0;
		
		LinkedList<Integer> fib = getFibonacci(4000000);
		
		for(int i = 0; i < fib.size();i++)
		{
			int term = fib.get(i);
			if(term % 2 == 0)
			{
				sum += term;
			}
		}
		
		System.out.println("The sum of even terms is: " + sum);
	}

	public static LinkedList<Integer> getFibonacci(int max)
	{
		LinkedList<Integer> fib = new LinkedList<Integer>();
		fib.add(1);
		fib.add(2);
		
		while (fib.getLast() <= max)
		{
			int num = fib.get(fib.size()-1) + fib.get(fib.size()-2);
			if(num<=max)
				fib.add(num);
			else
				break;
		}
		
		return fib;
	}
}
